package com.hnas.account.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnas.account.orm.Account;
import com.hnas.account.service.AccountService;
import com.hnas.account.service.dto.DTOAccount;
import com.hnas.account.service.dto.search.DTOAccountSearch;
import com.hnas.account.service.mapper.AccountMapper;
import com.hnas.account.util.AccountConstant;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.dto.DataBean;

@Controller
@RequestMapping("/backend/account")
public class AccountController {
	
	@Autowired
	private AccountService accountService;
	
	/**
	 * 用户列表
	 * @param modelMap
	 * @param request
	 * @param response
	 * @param criteria
	 * @return
	 */
	@RequestMapping(value = "/search")
	public String search(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOAccountSearch criteria){
		criteria.setTotalCount(-1);
		DTOPagedList<DTOAccount> pg = this.accountService.search(criteria);
		modelMap.put("pg", pg);
		modelMap.put("dto", criteria);
		
		List<DataBean> beans = new ArrayList<DataBean>();//控制页面表格显示的列
		beans.add(new DataBean("empId","empId","员工编号",false,150,true));		
		beans.add(new DataBean("phoneNo","phoneNo","手机号",false,150,true));
		beans.add(new DataBean("accName","accName","域账号",false,150,true));
		beans.add(new DataBean("userName","userName","用户名",false,150,true));
		beans.add(new DataBean("openId","openId","openId",false,150,true));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("appTypeStr","appTypeStr","应用类型",false,150,true));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("loginTypeStr","loginTypeStr","登录类型",false,170,true));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("appId","appId","应用id",false,150,true));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("eleCardNum","eleCardNum","电子卡数量",false,150,true));
		beans.add(new DataBean("unclaimedEleCardNum","unclaimedEleCardNum","待确认卡数量",false,150,true));
		beans.add(new DataBean("lastLoginTime","lastLoginTime","最后登录时间",false,150,true));
		beans.add(new DataBean("appTokenValidEndTime","appTokenValidEndTime","海贝APP_Token有效截止时间",false,150,true));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("appToken","appToken","海贝APP_Token",false,150,true));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("wxTokenValidEndTime","wxTokenValidEndTime","微信APP_Token有效截止时间",false,150,true));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("validStr","validStr","是否启用",false,150,true));
		beans.add(new DataBean("createTime","createTime","注册时间",false,150,true));
		beans.add(new DataBean("updateTime","updateTime","更新时间",false,150,true));
		beans.get(beans.size() - 1).setDisplay(false);
		modelMap.put("beans", beans);
		modelMap.put("validMap", AccountConstant.getValidMap());
		
		return "/backend/account/list_account";
	}
	
	/**
	 * 展示详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/show", method = RequestMethod.POST)
	@ResponseBody
	public DTOAccount show(String id) {
		DTOAccount dto = AccountMapper.getInstance().mapToValue(accountService.findById(id));
		dto = accountService.setCardInfo(dto);
		return dto;
	}
	
	/**
	 * 更新账户有效性
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/updateAccount", method = RequestMethod.POST)
	@ResponseBody
	public String updateAccount(String id,String valid) {
		String flag = "0";//默认返回失败
		Account account = accountService.findById(id);
		if(null != account){
			if(AccountConstant.VALID.equals(valid)){
				valid = AccountConstant.NOT_VALID;
			}else{
				valid = AccountConstant.VALID;
			}
			account.setValid(valid);
			try {
				account = accountService.saveOrUpdate(AccountMapper.getInstance().mapToValue(account));
				if(null != account){
					flag = "1";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return flag;
	}
	

	
}
